﻿using Aspfm.Main;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Aspfm.FtpFile
{
    public class FtpControl : Aspfm.Main.IMainController
    {
        public override void MvcController(string active)
        {
            if (active == "read")
            {
                string fileid = RequestDynObj.GetPropertyValueStr("fileid");
                if (!string.IsNullOrEmpty(fileid))
                {
                    ReadFile(fileid);
                }
                else
                {
                    string filename = RequestDynObj.GetPropertyValueStr("filename");
                    if (!string.IsNullOrEmpty(filename))
                    {
                        ReadTempFile(filename);
                    }
                }
            }
            else if (active == "save")
            {
                SaveFile();
            }
        }
        public void ReadFile(string fileid)
        {
            WebResult webr = AspfmFileRead.GetFileId(fileid);
            string filename = _Path + webr.Result.GetPropertyValueStr("filename");
            base.ReturnFile(filename);
        }
        public void ReadTempFile(string filename)
        {
            filename = _Path + AspfmFileRead.TempPath+"/"+ filename;
            base.ReturnFile(filename);
        }

        public void SaveFile()
        {
            System.Web.HttpFileCollection files = GetHttpFiles();
            if (files != null && files.Count > 0)
            {
                List<WebResult> bweblist = new List<WebResult>();
                string ftpkey = RequestDynObj.GetPropertyValueStr("ftpkey");
                for (int i = 0; i < files.Count; i++)
                {
                    WebResult webres = AspfmFileSave.SaveFile(files[i], ftpkey);
                    bweblist.Add(webres);
                }
                base.ReturnObj(bweblist);
            }
        }
    }
}
